# -*-coding:utf-8-*-
import pika
from queue import Queue
import time
import threading
# import stomp
import json

class MQ_in():
    def __init__(self):
        self.q = Queue()
        self.hostname = 'admin'
        self.password = '1MKXQUIN'
        self.IP = '117.34.118.30'
        self.port = 25672
        self.queue = 'pos.bas.*.*'
    #---class SampleListener(object)-----  
    def callback(self, channel, method, properity, body):
        #channel.basic_ack(delivery_tag = method.delivery_tag)
        x = body.decode()
        print(x)
        self.q.put(x)
    #---------
    def receiver(self):
        while True:
            try:
                credient = pika.PlainCredentials(username = self.hostname,
                                                 password = self.password)
                connection = pika.BlockingConnection(
                    pika.ConnectionParameters(
                        host= self.IP,
                        port= self.port,
                        virtual_host='/',
                        credentials=credient))
                channel = connection.channel()
                channel.basic_consume(
                    queue= self.queue,
                    on_message_callback=self.callback,
                    auto_ack=True) 
                channel.start_consuming()
            except:
                time.sleep(5)
                continue




class MyListener():
    def __init__(self):
        self.q = Queue()
    def on_message(self, headers, msg):
        data_in = {}
        result = {}
        s = json.loads(msg)
        sa = s['map']
        sb = sa['entry']
        for i in sb:
            result[i['string'][0]] = i['string'][1]
            try:
                data_in['TIME'] = result['POSITION_UP_TIME']
                data_in['LUID']        = result['LINE_UUID']
                data_in['LTYPE']        = result['LINE_TYPE']
                data_in['BUS_UID']    = result['HED_BUS_PALTE']
                data_in['LNAME'] = result['POSITION_LINE_NAME']
                #data_in['HED_BUS_UUID']     = result['HED_BUS_UUID']
                data_in['SPEED']   = result['POSITION_SPEED']
                data_in['IOSAT'] = result['ENTER_OUT_SATION']
                data_in['STA_SEQ'] = result['POSITION_STATION_SEQ']
                data_in['LNG'] = result['POSITION_LONGITUDE']
                data_in['LAT']    = result['POSITION_LATITUDE']
                data_in['RATE'] = float(result['POSITION_STATION_RATE'])
                
                data_in['POSITION_LINE_NAME'] = result['POSITION_LINE_NAME']
                self.q.put(data_in)
            except:
                pass
#
# def receive_from_topic():
#     conn = stomp.Connection10(host_and_ports = [('192.168.200.26', 60603)])
#     conn.set_listener('', MyListener())
#     conn.start()
#     conn.connect('Admin','')
#     conn.subscribe(destination='/topic/tpc.out.cic', headers={'transformation' : 'jms-map-json'})
#     print("Waiting for messages...")
#     while True:
#         time.sleep(1)



#=========

def test():
    mq = MQ_in() 
    t1 = threading.Thread(target = mq.receiver)
    t1.start()
test()

